package com.zhouzi.zhaoyang.util;

import android.util.Log;

import java.math.BigDecimal;
import java.text.DecimalFormat;

/**
 * Created by Lucien on 2017/7/31. 金额计算器
 */
public class Calculator {

    public final static double AMOUNT_MAX_VALUE = 999999.99d;
    private static final String TAG = "Calculator";
    /*常量*/
    private static final String FORMAT = "#.##";
    private static final String ZERO = "0";
    private static final String DOT = ".";
    private static final double MAX_LIMIT = 1000000d;
    /*缓存*/
    private static StringBuilder inpBuf = new StringBuilder();/*输入缓存*/
    private static StringBuilder addbuf = new StringBuilder();/*累加缓存*/

    /*按键事件*/
    public static String onKeyEvent(char keycode) {
        Log.d(TAG, "onKeyEvent [" + keycode + "]");
        switch (keycode) {
            case KeyCode._0:
            case KeyCode._1:
            case KeyCode._2:
            case KeyCode._3:
            case KeyCode._4:
            case KeyCode._5:
            case KeyCode._6:
            case KeyCode._7:
            case KeyCode._8:
            case KeyCode._9:
            case KeyCode._D: {
                return check(keycode);
            }
            case KeyCode._P:
                return doAdd();
            case KeyCode._E: {
                return doSum();
            }
            case KeyCode._C: {
                return reset();
            }
            case KeyCode._B: {
                return doBackSpace();
            }
        }
        return "";
    }

    /*检测*/
    private static String check(char keycode) {
        if (inpBuf.length() == 0) {
            if (keycode == KeyCode._D)
                inpBuf.append(ZERO);
            inpBuf.append(keycode);
        } else {
            /*是否已经输入过小数点*/
            if (inpBuf.toString().contains(DOT)) {
                /*如果输入的不是小数点，小数点后面最多输入两位*/
                if (keycode != KeyCode._D && inpBuf.length() - inpBuf.indexOf(DOT) <= 2)
                    inpBuf.append(keycode);
                /*如果继续输入的是小数点 则忽略*/
            } else {
                if (inpBuf.charAt(0) == KeyCode._0 && keycode != KeyCode._D) /*如果是已输入0,在输入非小数点，则删除第一个0*/
                    inpBuf.deleteCharAt(0);
                if (inpBuf.length() == 0 || Double.parseDouble(inpBuf.toString()) < MAX_LIMIT)
                    inpBuf.append(keycode);
            }
        }
        return inpBuf.toString();
    }

    public static String reset(String amt) {
        inpBuf = new StringBuilder();
        addbuf = new StringBuilder(amt);
        return amt;
    }

    /*复位*/
    public static String reset() {
        inpBuf = new StringBuilder();
        addbuf = new StringBuilder();
        return ZERO;
    }

    /*求和*/
    public static String doSum() {
        DecimalFormat df2 = new DecimalFormat(FORMAT);
        BigDecimal augend = new BigDecimal(0);
        BigDecimal addend = new BigDecimal(0);

        if (inpBuf.length() > 0)
            augend = new BigDecimal(inpBuf.toString());

        if (addbuf.length() > 0)
            addend = new BigDecimal(addbuf.toString());

        augend = augend.add(addend);

        addbuf = new StringBuilder();
        inpBuf = new StringBuilder(df2.format(augend.doubleValue()));/*求和完成后 输入缓存 清空*/
        return inpBuf.toString();
    }

    /*求和*/
    public static String doAdd() {
        DecimalFormat df2 = new DecimalFormat(FORMAT);
        BigDecimal augend = new BigDecimal(0);
        BigDecimal addend = new BigDecimal(0);

        if (inpBuf.length() > 0)
            augend = new BigDecimal(inpBuf.toString());

        if (addbuf.length() > 0)
            addend = new BigDecimal(addbuf.toString());

        augend = augend.add(addend);

        addbuf = new StringBuilder(df2.format(augend.doubleValue()));
        inpBuf = new StringBuilder();/*求和完成后 输入缓存 清空*/
        return addbuf.toString();
    }

    /*退格*/
    private static String doBackSpace() {
        if (inpBuf.length() > 0) {
            inpBuf.deleteCharAt(inpBuf.length() - 1);
            if (inpBuf.length() != 0) {
                return inpBuf.toString();
            } else {
                if (addbuf.length() != 0)
                    return addbuf.toString();
                else
                    return ZERO;
            }
        }
        if (addbuf.length() > 0) {
            addbuf.deleteCharAt(addbuf.length() - 1);
            if (addbuf.length() != 0)
                return addbuf.toString();
            else
                return ZERO;
        }
        return ZERO;
    }

    public static int parserAmount(String amountStr) {
        try {
            return (int) (Double.parseDouble(amountStr) * (double) 100);
        } catch (Exception e) {
            return 0;
        }
    }

    public static String fen2Yuan(long amt) {
        DecimalFormat df2 = new DecimalFormat("0.00");
        BigDecimal augend = new BigDecimal(amt);
        BigDecimal addend = new BigDecimal(100);
        augend = augend.divide(addend);
        return df2.format(augend.doubleValue());
    }

    public static Long yuan2Fen(String amt) {
        return new BigDecimal(amt).multiply(new BigDecimal(100)).setScale(0, BigDecimal.ROUND_HALF_UP).longValue();
    }

    public static String fen2YuanRmb(long amt) {
        DecimalFormat df2 = new DecimalFormat("0.00");
        BigDecimal augend = new BigDecimal(amt);
        BigDecimal addend = new BigDecimal(100);
        augend = augend.divide(addend);
        return "¥" + df2.format(augend.doubleValue());
    }

    /*键码定义*/
    public interface KeyCode {
        char _0 = '0';
        char _1 = '1';
        char _2 = '2';
        char _3 = '3';
        char _4 = '4';
        char _5 = '5';
        char _6 = '6';
        char _7 = '7';
        char _8 = '8';
        char _9 = '9';
        char _D = '.';//    .

        char _P = '+';//    +
        char _E = '=';//    =
        char _C = 'c';//    Clr
        char _B = 'b';//    BackSpace
    }


}
